package com.java.exer_3;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class TestCalendar_2 {
    public static SimpleDateFormat sdf = new SimpleDateFormat("dd\t");

    public static void main(String[] args) {
        for (int i = 1; i <= 12; i++) {
            printMonthCalendar(i);
            System.out.println();
        }
    }

    public static void printMonthCalendar(int month) {
        //修正月份为0~11
        month--;
        //输入当月日历
        //定义当前日历对象
        GregorianCalendar nowGc = new GregorianCalendar();
        //设置前月份
        nowGc.set(Calendar.MONTH, month);
        //设置日历对象从当月1日开始
        nowGc.set(Calendar.DAY_OF_MONTH, 1);
        //System.out.println(nowGc.getTime().toLocaleString());
        //打印月份
        System.out.println("-- " + (month + 1) + "月 ----------------------");
        //打印周
        System.out.println("日\t一\t二\t三\t四\t五\t六");
        //补足1号之前的空白天
        for (int i = 0; i < nowGc.get(Calendar.DAY_OF_WEEK) - 1; i++) {
            System.out.print("\t");
        }
        while (nowGc.get(Calendar.MONTH) == month) {
            //获取日
            // int day = nowGc.get(Calendar.DAY_OF_MONTH);
            //打印日
            //System.out.print(day+"\t");
            System.out.print(sdf.format(nowGc.getTime()));
            //判断是否周六，如果是换行
            if (nowGc.get(Calendar.DAY_OF_WEEK) == 7) {
                System.out.println();
            }
            //累加日
            nowGc.add(Calendar.DAY_OF_MONTH, 1);
        }
    }
}
